package com.knight.hadoop.day10.preprocess;

import java.io.IOException;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

/**
 * web日志预处理
 *
 */
public class WebLogpreprocess {
	
	
	static class WebLogpreprocessMapper extends Mapper<LongWritable, Text, Text, NullWritable>{
		
		Text k = new Text();
		
		@Override
		protected void map(LongWritable key, Text value, Context context)
				throws IOException, InterruptedException {
			String line = value.toString();
			WebLogObject webLogObject = WebLogParser.parse(line);
			
			k.set(webLogObject.toString());
			context.write(k, NullWritable.get());
			
		}
	}
	
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		job.setJarByClass(WebLogObject.class);
		job.setMapperClass(WebLogpreprocessMapper.class);
		
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(NullWritable.class);
		
		
		FileInputFormat.setInputPaths(job, new Path("E:\\home\\webLog\\input"));
		FileOutputFormat.setOutputPath(job, new Path("E:\\home\\webLog\\output"));
		
		boolean res = job.waitForCompletion(true);
		
		System.exit(res?0:1);
	}
}
